package edu.whu.notepal.controller;

import edu.whu.notepal.dto.Result;
import edu.whu.notepal.pojo.Group;
import edu.whu.notepal.service.GroupService;
import edu.whu.notepal.utils.RedisClient;
import edu.whu.notepal.utils.RedisConstants;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 群组的控制器
 * @author 陈磊
 */
@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    GroupService groupService;

    @Resource
    RedisClient redisClient;

    @GetMapping("/{id}")
    public Result<Group> getByGroupId(@PathVariable("id")Long id){
        Group group = redisClient.queryWithPassThrough(RedisConstants.GROUP_INFO_KEY, id, Group.class,
                groupService::getById, RedisConstants.GROUP_INFO_TTL, TimeUnit.MINUTES);
        return Result.success(group);
    }

    @PutMapping
    public Result<String> updateName(@RequestBody Group group){
        redisClient.deleteKey(RedisConstants.GROUP_INFO_KEY, group.getNoteId());
        groupService.updateById(group);
        return Result.success("更改成功！");
    }

}